/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ipartek.formacion.gestformwww.controlador;

import com.ipartek.formacion.gestformwww.modelo.ModeloCurso;
import com.ipartek.formacion.gestformwww.modelo.beans.AlumCurso;
import com.ipartek.formacion.gestformwww.modelo.beans.excepciones.GestorFormacionExcepcion;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Alvaro,Borja y Karmele
 */
public class ModificarInfoAccion implements Accion {

   
    private final String visualizaralumno = "visualizar-alumno-accion";
    private final String listarcursos = "listar-cursos-accion";

    public AccionRouter ejecuta(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) {
       HttpSession sesion = request.getSession();
        AccionRouter router = null;
          AccionServlet as=(AccionServlet) servlet;
        ArrayList<String> mensajes = new ArrayList<String>();
        int idCurso;
        boolean ejecutar = true;
        ArrayList<String> errorm = new ArrayList<String>();
        ModeloCurso modelo = (ModeloCurso) servlet.getServletContext().getAttribute("ModeloCurso");
        AlumCurso alu = new AlumCurso();


        ejecutar = Verificador.sinReenvio("aleatorioModificarInfo", sesion, request);
        

        if (sesion.getAttribute("usuario") != null && request.getMethod().equalsIgnoreCase("POST")) {
            if (Verificador.numeroValido(request.getParameter("idCurso"))) {
                idCurso = Integer.parseInt(request.getParameter("idCurso"));
                alu.setCurId(idCurso);
                alu.setAluId(Integer.parseInt(request.getParameter("idAlumno")));
                alu.setAvisos(request.getParameter("avisos"));
                alu.setFchUltiCla(request.getParameter("fchUltiCla"));
                alu.setObservaciones(request.getParameter("observaciones"));
                alu.setOrden(Integer.parseInt(request.getParameter("orden")));
                alu.setPracticas(request.getParameter("practicas"));
                alu.setReserva(request.getParameter("reserva"));
                alu.setTerminado(request.getParameter("terminado"));
                alu.setValFinal(request.getParameter("valFinal"));

                errorm = alu.validar();
                if (errorm.isEmpty()) {
                    if (ejecutar) {
                        try {
                            modelo.modificarInfoCurso(alu);
                            mensajes.add("modificar-info-correcto");
                            sesion.setAttribute("mensajes", mensajes);
                        } catch (GestorFormacionExcepcion ex) {
                            Logger.getLogger(ModificarInfoAccion.class.getName()).log(Level.SEVERE, null, ex);
                            sesion.setAttribute("excepciones", ex.getMessage());
                        }
                    }
                } else {
                    request.setAttribute("errores", errorm);
                }
                try {
                    router=as.getFactory().getAction(as.getNombreClaseRecurso(visualizaralumno),servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ModificarInfoAccion.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(ModificarInfoAccion.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(ModificarInfoAccion.class.getName()).log(Level.SEVERE, null, ex);
                }
            
            } else {
                try {
                    router=as.getFactory().getAction(as.getNombreClaseRecurso(listarcursos),servlet.getClass().getClassLoader()).ejecuta(servlet, request, response);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ModificarInfoAccion.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(ModificarInfoAccion.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(ModificarInfoAccion.class.getName()).log(Level.SEVERE, null, ex);
                }
               
            }
        } else {
            router = new AccionRouter("inicio");
        }
        return router;
        
    }

   

}
